/**
 * typeof
 */
let student = {
    id: 1,
    name: "xiaowang",
    address: "武侯区"
}

type sem = typeof student  //{id:number,name:string,address:string}

let student2: sem = {
    id: 2, name: "xiaowang", address: "高新区"
}


function toArray(x: number): number {
    return x
}

type funType = typeof toArray
interface IMessage {
    check: funType
}


/**
 * keyof
 */
interface IMessage {
    id: number,
    name: string
}

type K1 = keyof IMessage  // K1 = "id" | "name"
type K2 = keyof IMessage[] // K2 = "length" | "pop" | "push"

// 实际应用场景
interface IObj {
    id:number,
    name:string
}
function getKey(obj:IObj,key:string){
    // const key2 = "address"
    // const key2 = keyof IObj
    return obj[key as keyof IObj]
    return (obj as any)[key]
}

//获取指定的属性值，扩展性更高
function getValueByKey<T extends sem,K extends keyof T>(obj:T,key:K){
    return obj[key]
    obj.id
}
getValueByKey(student,"id")


const obj = {
    id:1,
    name:"xiaowang",
    weight:120,
    children:[
        1,2,3
    ]
}
//生成的约束就和obj一模一样
type objType = typeof obj
//{id?:string,name?:string,weight?:string}
//T:{id:number,name:string}
type objType2<T> = {
    [k in keyof T]?:T[k]
}
type objType3<T> = {
    [k in keyof T]?:string
}

const p1:objType3<objType> = {
    id:"2",
    name:"xiaofei",
    weight:"120",
    children:"[1,2,3]"
}










export { }